$(document).ready(function () {
  var iconMap = $('.icon-map');
  var title = "Harry Winston";
  var termsBtn = $('.terms');
  var point = [{ id: "mapbox1", lng: 121.571659, lat: 25.037544 }, { id: "mapbox2", lng: 121.535601, lat: 25.057426 }];
  
  // 弹窗1
  var popupPolicy = $('#popup-policy');
  var closeBtn = popupPolicy.find('.close');
  //弹窗2
  var popupReserve = $('#popup-reserve');
  var closeBtn2 = popupReserve.find('.alert-confirm');

  // 模拟下拉框
  var timeSelect = $('.timeSelect');

  //表单判断
  var selectShop = $('select[name=shop]');
  var selectHour = $('select[name=time]');
  var 
    inputAgree = $('#agree'),
    inputName = $('#name'),
    inputPhone = $('#phone'),
    inputEmail = $('#email'),
    inputShop = $('#shop'),
    inputDate = $('#date'),
    inputTime = $('#time');
  var btnSub = $('#btnSub');
  //页面初始化
  pageInit();

  function pageInit() {
    eventBind();
    shopInit(dateList);
    var Media = $("#media");
    monitor_handler();
  }//end func

  //事件绑定
  function eventBind() { 
    iconMap.on('click', iconMap_click);
    termsBtn.on('click', termsBtn_click);
    Select_handler();
    btnSub.on('click', btnSub_click);
    closeBtn.on('click', function () { popupPolicy.hide(); });
    closeBtn2.on('click', function () { popupReserve.hide(); });
  }//end func
  //显示地图
  function initialize(id,lng, lat) {
    // console.log(id, lng, lat);
    var myCenter = new google.maps.LatLng(lng, lat);
    var mapProp = {
      center: myCenter,
      zoom: 17,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
    };
    var map = new google.maps.Map(document.getElementById(id), mapProp);
    var marker = new google.maps.Marker({
      position: myCenter,
      title: 'Click to zoom',
    });
    marker.setMap(map);
    // Zoom to 9 when clicking on marker
    google.maps.event.addListener(marker, 'click', function () {
      map.setZoom(9);
      map.setCenter(marker.getPosition());
    });
  }



  function iconMap_click() {
    var that = $(this);
    var index = $(this).data('code');
    // var imap = new baidumap();
    $(this).parent().next('div.mapbox').slideToggle('slow', function () {
      initialize(point[index].id, point[index].lat, point[index].lng);
      // imap.init(point[index].id, point[index].lat, point[index].lng);
      // imap.addNewMarkers(title, point[index].lng, point[index].lat);
      google.maps.event.addDomListener(window, 'load', initialize);
    });
  }//end func

  function termsBtn_click(e){
    popupPolicy.show();
  }//end func

  // 模拟下拉框
  function Select_handler() {
    timeSelect.on('change', timeSelectChange);
  }//end func

  function timeSelectChange() {
    var timeText = $(this).parent().children('.timeText');
    timeText.html($(this).val());
  }//end func
  
  
  function btnSub_click(e) {
    if (inputName.val() === "") { fn.alert('請輸入姓名,謝謝');return;}
    if (!icom1.checkStr(inputEmail.val(), 2)) { fn.alert('請輸入正確的郵箱,謝謝'); return; }
    if (inputPhone.val() === "") { fn.alert('請輸入電話,謝謝'); return; }
    if (inputShop.html() === "請輸入分店......") { fn.alert('請輸入分店,謝謝'); return; }
    if (inputDate.val() === "請輸入日期......") { fn.alert('請輸入日期,謝謝'); return; }
    if (inputTime.html() === "請輸入時間......") { fn.alert('請輸入時間,謝謝'); return; }
    if (!inputAgree.is(':checked')) { fn.alert('請勾選同意條款,謝謝'); return; }
    //ajax写这里
    var data={};
    data.name = inputName.val();
    data.phone = inputPhone.val();
    data.email = inputEmail.val();
    data.shop = inputShop.html();
    data.date = inputDate.val();
    data.time = inputTime.html();
    console.log(data);
    loop.user.add({ name: inputName.val(), phone: inputPhone.val(), email: inputEmail.val(), address: inputShop.html(), remarks: (inputDate.val() + '-' +inputTime.html())}, function (data) {
      if (data.errcode == 0) {
        imonitor.add({ action: 'click', category: '预约页', label: '提交成功按钮' });	
        popupReserve.fadeIn();
        // fn.alert('提交成功');
      }
      else {
        fn.alert(data.errmsg);
      }

    });
  }//end func

  function shopInit(shopdata) {
    // console.log(shopdata);
    var html = '<option>請輸入分店......</option>';
    for(var i =0;i<shopdata.length;i++){
      html += '<option>' + shopdata[i].name+'</option>';
    }
    inputShop.next('select').html(html);
    $("[name='shop']").on('change', function () {
      inputDate.val('');
      inputTime.html('請輸入時間......');
      selectHour.empty();
      initDate();
    });
  }//end func
  function initDate() {
    //日历
    $("input[name=date]").datepicker({
      language: "zh-TW",
      autoclose: true,
      startView: 0,
      format: "yyyy-mm-dd",
      clearBtn: true,
      todayBtn: false,
      endDate: Infinity,
      todayHighlight: true
    }).on('changeDate', function (ev) {
      inputTime.html('請輸入時間......');
      selectHour.empty();
      if (ev.date) {
        var d = new Date(ev.date);
        youWant = (d.getMonth() + 1) + '-' + d.getDate();
        var sDate = Number(youWant.split('-').join(''));
        hour_creat(sDate);
      } else {}
    });
    //
    function GetDateStr(AddDayCount) {
      var dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期 
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1;//获取当前月份的日期 
      var d = dd.getDate();
      var mm,ddd;
      if (m < 10) {
        mm = "0" + m;
      }else{
        mm = m;
      }
      if (d < 10) {
        ddd = "0" + d;
      } else {
        ddd = d;
      }
      return y + "-" + mm + "-" + ddd;
    } 
    console.log(GetDateStr(1));
    //
    $("input[name=date]").datepicker('update');
    $("input[name=date]").datepicker('setStartDate', GetDateStr(1));
    $("input[name=date]").datepicker('setEndDate', '2018-06-19');

  }//end func
  function hour_creat(date) {
    console.log(date);
    var shopId = selectShop[0].selectedIndex - 1;
    var list = dateList[shopId];
    console.log(list);
    if (list.hours) hours_get(list.hours);
    else if (list.list) {
      var searchId = days_get(date, list.list);
      console.log('search id:' + searchId);
      hours_get(list.list[searchId].hours);
    }//end if
  }//edn func

  function hours_get(list) {
    var html = '<option>請輸入時間......</option>';
    for (var i = 0; i < list.length; i++) {
      html += '<option>' + list[i] + '</option>';
    }//end func
    selectHour.html(html);
  }//end func

  function days_get(date, list) {
    for (var i = 0; i < list.length; i++) {
      var start = list[i].start;
      var end = list[i].end;
      if (date >= start && date <= end) {
        return i;
        break;
      }//end if
    }//end func
  }//end func
 
  //----------------------------------------页面监测代码----------------------------------------
  function monitor_handler() {
    imonitor.add({ obj: $('.Thisvideo'), action: 'click', category: '视频', label: '视频播放按钮' });
  }//end func
});//end ready
